package com.jv;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;
import org.apache.shiro.mgt.SecurityManager;

/**
 * 在shiro-jdbc.ini文件中配置好jdbcRealm，jdbcRealm会默认从数据库中找users表，根据表中的username和password字段验证用户名和密码。
 */
public class TestA {

    public static void main(String[] args) {

        //[1]解析shiro.ini文件
        Factory<SecurityManager>  factory =new IniSecurityManagerFactory("classpath:shiro-jdbc.ini");

        //[2]通过SecurityManager工厂获得SecurityManager实例
        SecurityManager securityManager = factory.getInstance();

        //[3]把SecurityManager对象设置到运行环境中
        SecurityUtils.setSecurityManager(securityManager);

        //[4]通过SecurityUtils获得主体subject
        Subject subject = SecurityUtils.getSubject();

        //[5]书写自己输入的账号和密码---相当于用户自己输入的账号和密码
        UsernamePasswordToken  token =new UsernamePasswordToken("heizi","123");
        try {
            //[6]进行身份的验证

            subject.login(token);

            //[7]通过方法判断是否登录成功

            if(subject.isAuthenticated()){
                System.out.println("登录成功");

            }
        } catch (IncorrectCredentialsException e) {

            System.out.println("登录失败");


        }catch (UnknownAccountException e){

            System.out.println("用户名不正确");
        }
    }
}
